<template>
  <view>
    <!-- 模块1：收货地址 -->
    <view class="address">
      <!-- 正常情况：显示收货地址 -->
      <view v-if="address.provinceName" class="address-info">
        <view class="address-detail">{{ '用户收货地址' }}</view>
        <view class="address-user"> {{ '收货人' }} {{ '135****7057' }} </view>
      </view>
      <!-- 互斥情况：显示添加按钮 -->
      <view v-else class="address-btn" @tap="chooseAddress">添加地址</view>
      <!-- ==={{ addressInfo }} -->
    </view>
    <!-- 模块2：已选商品列表 -->
    <view class="goods-list">
      <view class="goods-title">已选商品</view>
      <view class="cart-item" v-for="item in selectCartList" :key="item">
        <view class="cart-count"> x{{ item.goods_count }} </view>
        <navigator class="goods-item" hover-class="none" :url="`/pages/index/index`">
          <!-- 商品图片 -->
          <image class="goods-image" :src="item.goods_small_logo" mode="aspectFit" />
          <!-- 商品信息 -->
          <view class="goods-info">
            <view class="goods-name">{{ item.goods_name }}</view>
            <view class="goods-price">{{ item.goods_price }}</view>
          </view>
        </navigator>
      </view>
    </view>
    <!-- 模块3：底部操作栏 -->
    <view class="bottom-bar">
      <view class="bottom-bar-fixed">
        <!-- 合计 -->
        <view class="total">
          <text class="total-name">合计：</text>
          <text class="total-price">{{ total }}</text>
        </view>
        <!-- 结算 -->
        <view class="account" @tap="goPay"> 去支付({{ totalCount }}) </view>
      </view>
    </view>
  </view>
</template>

<script>
import { mapGetters, mapState } from 'vuex';
export default {
  data () {
    return {
      //
      address: {}
    }
  },
  computed: {
    ...mapState('user', ['token']),
    ...mapGetters(['selectCartList', 'total', 'totalCount']),
    addressInfo () {
      // 详细地址拼接
      return this.address.provinceName + this.address.cityName + this.address.countyName + this.address.detailInfo
    }
  },
  methods: {
    chooseAddress () {
      uni.chooseAddress({
        success: (res) => {
          console.log(res, 9);
          this.address = res
        }
      })
    },
    async goPay () {
      //去支付
      /* 
      1-判断是否登录
      2-判断是否有收货地址
      3-判断是否有商品
      4-支付
      */
      if (!this.token) {
        uni.switchTab({
          url: '../my/my'
        })
        return uni.showToast({ title: '请登录' })
      }
      if (!this.address.provinceName) return uni.showToast({ title: '请填写收获地址' })
      if (this.selectCartList.length == 0) return uni.showToast({ title: '请选择商品再来支付' })
      /* 支付流程 */
      /*
      1-创建订单===获取订单编号 order_number
      2-根据订单编号获取支付参数  pay
      3-调用微信支付api传入pay参数实现微信支付
      */
      //  1-创建订单===获取订单编号 order_number
      const res = await uni.$u.http.request({
        url: '/my/orders/create',
        method: 'POST',
        data: {
          order_price: this.total,
          consignee_addr: this.addressInfo,
          goods: this.selectCartList.map(item => {
            return {
              goods_id: item.goods_id,
              goods_price: item.goods_price,
              goods_number: item.goods_count,
            }
          })
        }
      })
      const order_number = res.data.message.order_number
      //   2-根据订单编号获取支付参数  pay
      const res2 = await uni.$u.http.request({
        url: '/my/orders/req_unifiedorder',
        method: 'POST',
        data: {
          order_number
        }
      })
      console.log(res2, 9988);
      const payObj = res2.data.message.pay
      // 3-调用微信支付api传入pay参数实现微信支付
      wx.requestPayment({
        ...payObj,
        success (res3) {
          console.log(res3, 99);
        },
        fail (res) { }
      })
    }
  }
}
</script>

<style lang="scss">
page {
  background-color: #f4f4f4;
}

// 模块1：收货地址
.address {
  background-color: #fff;
  margin: 20rpx;
  padding: 20rpx;
  border-radius: 10rpx;
  display: flex;
  justify-content: center;
  align-items: center;

  .address-info {
    width: 100%;

    .address-detail {
      font-size: 30rpx;
      font-weight: bold;
      margin-bottom: 20rpx;
    }
  }

  .address-btn {
    width: 303rpx;
    height: 90rpx;
    border-radius: 45rpx;
    display: flex;
    justify-content: center;
    align-items: center;
    border: 1px solid #eb4450;
    color: #eb4450;
  }
}

// 模块2：已选商品列表
.goods-list {
  background-color: #fff;
  margin: 20rpx;
  padding: 20rpx;
  border-radius: 10rpx;

  .goods-title {
    line-height: 40rpx;
    font-size: 26rpx;
  }

  .cart-item {
    position: relative;

    .cart-count {
      position: absolute;
      right: 20rpx;
      bottom: 30rpx;
    }

    // 选中最后一项
    &:last-child {
      .goods-item {
        border: none;
      }
    }
  }
}

// 全局公共样式
.goods-list {
  .goods-item {
    display: flex;
    border-bottom: 1rpx solid #ddd;
    padding: 25rpx;

    .goods-image {
      width: 190rpx;
      height: 190rpx;
    }

    .goods-info {
      flex: 1;
      font-size: 24rpx;
      // 垂直方向两端对齐
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      padding: 20rpx;

      .goods-name {
        // 多行超出隐藏
        overflow: hidden;
        -webkit-line-clamp: 2;
        text-overflow: ellipsis;
        display: -webkit-box;
        -webkit-box-orient: vertical;
      }

      .goods-price {
        color: #eb4450;

        // 前面的价格符号
        &::before {
          content: '￥';
          font-size: 80%;
          margin-right: 5rpx;
        }
      }
    }
  }
}

// 底部操作栏
.bottom-bar {
  // 占位用
  height: 100rpx;

  // 定位的操作栏
  .bottom-bar-fixed {
    height: 80rpx;
    position: fixed;
    z-index: 99;
    left: 0;
    right: 0;
    bottom: 0;

    border-top: 1px solid #eee;
    background-color: #fff;
    display: flex;
    align-items: center;
    padding: 0 20rpx;

    .select {
      .select-radio {
        font-size: 24rpx;
        color: #999;
      }
    }

    .total {
      flex: 1;
      display: flex;
      align-items: center;
      margin-left: 40rpx;

      .total-name {
        font-size: 24rpx;
      }

      .total-price {
        color: #eb4450;
      }
    }

    .account {
      background-color: #eb4450;
      color: #fff;
      font-size: 22rpx;
      width: 150rpx;
      height: 52rpx;
      border-radius: 26rpx;
      display: flex;
      justify-content: center;
      align-items: center;
    }
  }
}
</style>
